<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">
    function test_Handler_Keyboard_initialize(t) {
        t.plan(3);
        var control = new OpenLayers.Control();
        control.id = Math.random();
        var callbacks = {foo: "bar"};
        var options = {bar: "foo"};
        
        var oldInit = OpenLayers.Handler.prototype.initialize;
        
        OpenLayers.Handler.prototype.initialize = function(con, call, opt) {
            t.eq(con.id, control.id,
                 "constructor calls parent with the correct control");
            t.eq(call, callbacks,
                 "constructor calls parent with the correct callbacks");
            t.eq(opt, options,
                 "constructor calls parent with the correct options");
        }
        var handler = new OpenLayers.Handler.Keyboard(control, callbacks,
                                                      options);

        OpenLayers.Handler.prototype.initialize = oldInit;
    }

    function test_Handler_Keyboard_destroy(t) {
        t.plan(3);
        var control = new OpenLayers.Control();        
        var handler = new OpenLayers.Handler.Keyboard(control);
        var old = OpenLayers.Handler.prototype.destroy;
        t.ok(handler.eventListener != null,
             "eventListener is not null before destroy");
        OpenLayers.Handler.prototype.destroy = function() {
            t.ok(true, "destroy calls destroy on correct parent");
        };
        handler.destroy();
        t.ok(handler.eventListener == null,
             "eventListeners is null after destroy");
        OpenLayers.Handler.prototype.destroy = old;
    }

    function test_Handler_Keyboard_activate(t) {
        t.plan(15);

        var log;
        var map = new OpenLayers.Map('map');
        var control = new OpenLayers.Control();
        map.addControl(control);
        var handler = new OpenLayers.Handler.Keyboard(control);

        // mock OpenLayers.Event.observe
        var old = OpenLayers.Event.stopObserving;
        OpenLayers.Event.observe = function(obj, type, method) {
            log[type] = obj;
            var validType = OpenLayers.Util.indexOf(["keydown", "keyup"], type) != -1;
            t.ok(validType, "activate calls observe for " + type);
            t.ok(method == handler.eventListener,
                 "activate calls observing with correct method");
        };

        handler.active = true;
        var activated = handler.activate();
        t.ok(!activated,
             "activate returns false if the handler was already active");

        log = {};
        handler.active = false;
        handler.observeElement = map.div;
        activated = handler.activate();
        t.ok(log['keydown'] == map.div,
             "activate calls observing for keydown with correct object");
        t.ok(log['keyup'] == map.div,
             "activate calls observing for keyup with correct object");
        t.ok(activated,
             "activate returns true if the handler was not already active");

        log = {};
        handler.active = false;
        handler.observeElement = null;
        activated = handler.activate();
        t.ok(log['keydown'] == document,
             "activate calls observing for keydown with correct object");
        t.ok(log['keyup'] == document,
             "activate calls observing for keyup with correct object");
        t.ok(activated,
             "activate returns true if the handler was not already active");

        OpenLayers.Event.observe = old;
        map.destroy();
    }

    function test_Handler_Keyboard_deactivate(t) {
        t.plan(15);

        var log;
        var map = new OpenLayers.Map('map');
        var control = new OpenLayers.Control();
        map.addControl(control);
        var handler = new OpenLayers.Handler.Keyboard(control);

        // mock OpenLayers.Event.stopObserving
        var old = OpenLayers.Event.stopObserving;
        OpenLayers.Event.stopObserving = function(obj, type, method) {
            log[type] = obj;
            var validType = OpenLayers.Util.indexOf(["keydown", "keyup"], type) != -1;
            t.ok(validType, "deactivate calls stopObserving for " + type);
            t.ok(method == handler.eventListener,
                 "deactivate calls stopObserving with correct method");
        };

        handler.active = false;
        var deactivated = handler.deactivate();
        t.ok(!deactivated,
             "deactivate returns false if the handler was not already active");

        log = {};
        handler.active = true;
        handler.observeElement = map.div;
        deactivated = handler.deactivate();
        t.ok(log['keydown'] == map.div,
             "deactivate calls stopObserving for keydown with correct object");
        t.ok(log['keyup'] == map.div,
             "deactivate calls stopObserving for keyup with correct object");
        t.ok(deactivated,
             "deactivate returns true if the handler was active already");

        log = {};
        handler.active = true;
        handler.observeElement = document;
        deactivated = handler.deactivate();
        t.ok(log['keydown'] == document,
             "deactivate calls stopObserving for keydown with correct object");
        t.ok(log['keyup'] == document,
             "deactivate calls stopObserving for keyup with correct object");
        t.ok(deactivated,
             "deactivate returns true if the handler was active already");

        OpenLayers.Event.stopObserving = old;
        map.destroy();
    }


  </script>
</head>
<body>
    <div id="map" style="width: 300px; height: 150px;"/>
</body>
</html>
